<?php
/**
 * Created by PhpStorm.
 * User: Yieong
 * Date: 14/10/29
 * Time: 下午8:20
 */

class RetainController extends AdminBaseController {

    /**
     * 多日留存报表
     */
    public function index(){
    	$user = AccAuth::me();
        $this->setTitle('多日留存报表');

        $filter = new PubFilter();
        $grid   = new PubGrid();   
        
        $lod_special_id = $this->R('special_id');
        $lodos = $this->R('os');
        $filter->addSpecialFilter(false,2);
        $filter->addOsFilter($lod_special_id,true,2)
            ->addServerFilter($lod_special_id,$lodos,true,false,2)
            ->addFilter('time_start', 'input', '时间', array('date' => 'day', 'value' => date('Y-m-d', strtotime('-7 day'))),2)
            ->addFilter('time_end', 'input', '至', array('date' => 'day', 'value' => date('Y-m-d', strtotime('-1 day'))),2)
            ->addExport();

        $params = $filter->get();
        
        $params = $this->indexCommon($params);
        
        $params['kpi'] = 'RegUser';

        $header = RetainService::indexHeader($params);
        $data   = RetainService::indexData($params);

        if($filter->isExport()){
            $filename = LibExcel::getFilename('多日留存报表', $params);
            LibExcel::exportCsv($header, $data, $filename);
        }else{
            $grid->addGrid($header, $data, array(
                'order' => array('RecDate', 'desc'),
            	'length'=>100,
            	'fix'   => array(
            		'col' => 1
            	)
            ));

            $this->out['filter'] = $filter->show();
            $this->out['grid'] = $grid->show();
            $this->out['is_ana'] = 2;
        }
    }

    public function channel(){
    	$user = AccAuth::me();
        $this->setTitle('渠道用户留存');

        $filter = new PubFilter();
        $grid   = new PubGrid();

        $dayList = RetainService::dayList(true);

        $lod_special_id = $this->R('special_id');
		$lodos = $this->R('os');
		$filter->addSpecialFilter(false);
		$filter->addOsFilter($lod_special_id)
            ->addServerFilter($lodos,true)
            ->addFilter('time_start', 'input', PubFunc::t('时间'), array('date' => 'day', 'value' => date('Y-m-d', strtotime('-7 day'))))
            ->addFilter('time_end', 'input', PubFunc::t('至'), array('date' => 'day', 'value' => date('Y-m-d', strtotime('-1 day'))))
            ->addFilter('day', 'checkbox', '', array('options' => $dayList, 'value' => array(1,3,7)))
            ->addExport();
        
//         if($user['IsAdmin'] == 1){  //管理员才能操作
//         	$filter->userTypes();
//         }

        $params = $filter->get();
       
        $options = PubAuthSession::get('os');
        $os_array = array();
        foreach ($options as $key=>$val){
        	$os_array[] = $key;
        }
        if(!in_array($params['os'], $os_array)){
        	unset($params['os']);
        }
        
        $params['kpi'] = 'RegUser';

        $header = RetainService::channelHeader($params);
        $data   = RetainService::channelData($params);

        if($filter->isExport()){
            $filename = LibExcel::getFilename('渠道用户留存', $params);
            LibExcel::exportCsv($header, $data, $filename);
        }else{
            $grid->addGrid($header, $data, array(
                'order' => array('RecDate', 'desc'),
            ));

            $this->out['filter'] = $filter->show();
            $this->out['grid'] = $grid->show();

        }
    }

    public function role(){
        $this->setTitle('职业用户活跃留存');

        $filter = new PubFilter();
        $grid   = new PubGrid();

        $dayList = RetainService::dayList(true);

        $lod_special_id = $this->R('special_id');
		$lodos = $this->R('os');
		$filter->addSpecialFilter(false);
		$filter->addOsFilter($lod_special_id)
            ->addServerFilter($lodos,true)
            ->addFilter('time_start', 'input', PubFunc::t('时间'), array('date' => 'day', 'value' => date('Y-m-d', strtotime('-7 day'))))
            ->addFilter('time_end', 'input', PubFunc::t('至'), array('date' => 'day', 'value' => date('Y-m-d', strtotime('-1 day'))))
            ->addFilter('day', 'checkbox', '', array('options' => $dayList, 'value' => array(1,3,7)))
            ->addExport();


        $params = $filter->get();
        
        $options = PubAuthSession::get('os');
        $os_array = array();
        foreach ($options as $key=>$val){
        	$os_array[] = $key;
        }
        if(!in_array($params['os'], $os_array)){
        	unset($params['os']);
        }
        
        $params['kpi'] = 'RegUser';

        $header = RetainService::roleHeader($params);
        $data   = RetainService::roleData($params);

        if($filter->isExport()){
            $filename = LibExcel::getFilename('渠道用户留存', $params);
            LibExcel::exportCsv($header, $data, $filename);
        }else{
            $grid->addGrid($header, $data, array(
                'order' => array('RecDate', 'desc'),
            ));

            $this->out['filter'] = $filter->show();
            $this->out['grid'] = $grid->show();

        }
    }
} 